package com.example.service.impl;

import com.example.mapper.AreaMapper;
import com.example.mapper.UserAreaGroupMapper;
import com.example.pojo.Area;
import com.example.pojo.AreaLevel;
import com.example.pojo.Poi;
import com.example.pojo.UserAreaGroup;
import com.example.service.UserAreaGroupService;
import com.example.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserAreaGroupServiceImpl implements UserAreaGroupService {

    @Autowired
    private UserAreaGroupMapper userAreaGroupMapper;
    @Autowired
    private AreaMapper areaMapper;

    @Override
    public String addSelfGroup(String poiPath) {
        List<Poi> poiList = ExcelUtils.readExcel(poiPath, Poi.class);
        if(poiList==null){
            return "文件不存在";
        }
        List<UserAreaGroup> groupList=new ArrayList<>();
        int num=0;
        try {
            for (Poi poi : poiList) {
                if(poi.getUserId()==null||poi.getUserId()==-3||(poi.getCategory()!=null&&poi.getCategory()==2)){
                    continue;
                }
                UserAreaGroup userAreaGroup=new UserAreaGroup();
                userAreaGroup.setUserId(Long.valueOf(poi.getUserId()));
                userAreaGroup.setAreaId(Long.valueOf(poi.getAreaId()));
                userAreaGroup.setVillageId(Long.valueOf(poi.getVillageId()));
                userAreaGroup.setGroupId(Long.valueOf(poi.getGroupId()));
                userAreaGroup.setDelFlag(0);
                groupList.add(userAreaGroup);
                if(groupList.size()%1000==0){
                    num+=userAreaGroupMapper.batchInsert(groupList);
                    groupList.clear();
                }
            }
            if(groupList.size()>0){
                num+=userAreaGroupMapper.batchInsert(groupList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "成功（插入小组："+num+"条）";
    }

    @Override
    public String addStreetGroup(String poiPath) {
        List<Poi> poiList = ExcelUtils.readExcel(poiPath, Poi.class);
        if(poiList==null){
            return "文件不存在";
        }
        List<UserAreaGroup> groupList=new ArrayList<>();
        int num=0;
        try {
            for (Poi poi : poiList) {
                if(poi.getUserId()==null||poi.getUserId()==-3/*||(poi.getCategory()!=null&&poi.getCategory()==2)*/){
                    continue;
                }
                UserAreaGroup userAreaGroup=new UserAreaGroup();
                userAreaGroup.setUserId(Long.valueOf(poi.getUserId()));
                userAreaGroup.setAreaId(Long.valueOf(poi.getAreaId()));
                userAreaGroup.setVillageId(Long.valueOf(poi.getVillageId()));
                Area area = areaMapper.selectById(poi.getVillageId());
                Area area1 = areaMapper.selectByAreaNameAndParentId(area.getAreaName()+"换书小组",poi.getVillageId());
                if(area1==null){
                    area1 = areaMapper.selectByAreaNameAndParentId(area.getAreaName(),poi.getVillageId());
                }
                userAreaGroup.setGroupId(Long.valueOf(area1.getId()));
                userAreaGroup.setDelFlag(0);
                groupList.add(userAreaGroup);
                if(groupList.size()%1000==0){
                    num+=userAreaGroupMapper.batchInsert(groupList);
                    groupList.clear();
                }
            }
            if(groupList.size()>0){
                num+=userAreaGroupMapper.batchInsert(groupList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "成功（插入小组"+num+"条）";
    }

    @Override
    public String addCommunityGroup(String poiPath,String areaPath) {
        List<Poi> poiList = ExcelUtils.readExcel(poiPath, Poi.class);
        List<AreaLevel> areaLevelList = ExcelUtils.readExcel(areaPath, AreaLevel.class);
        if(poiList==null||areaLevelList==null){
            return "文件不存在";
        }
        List<UserAreaGroup> groupList=new ArrayList<>();
        List<Poi> pois=new ArrayList<>();
        int num=0;
        try {
            for (Poi poi : poiList) {
                if(poi.getUserId()==null||poi.getUserId()==-3||poi.getCommunityId()==null||(poi.getCategory()!=null&&poi.getCategory()==2)){
                    continue;
                }
                for (AreaLevel areaLevel : areaLevelList) {
                    if(!poi.getCommunityId().equals(areaLevel.getCommunityId())){
                        continue;
                    }
                    if(!poi.getAreaId().equals(areaLevel.getAreaId())){
                        pois.add(poi);
                        break;
                    }
                    UserAreaGroup userAreaGroup=new UserAreaGroup();
                    userAreaGroup.setUserId(Long.valueOf(poi.getUserId()));
                    userAreaGroup.setAreaId(Long.valueOf(poi.getAreaId()));
                    userAreaGroup.setVillageId(Long.valueOf(areaLevel.getVillageId()));
                    userAreaGroup.setGroupId(Long.valueOf(poi.getCommunityId()));
                    userAreaGroup.setDelFlag(0);
                    groupList.add(userAreaGroup);
                    if(groupList.size()%1000==0){
                        num+=userAreaGroupMapper.batchInsert(groupList);
                        groupList.clear();
                    }
                    break;
                }
            }
            if(groupList.size()>0){
                num+=userAreaGroupMapper.batchInsert(groupList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ExcelUtils.writeExcel(poiPath.substring(0,poiPath.lastIndexOf("."))+"-社区不在同区县下.xlsx",pois, Poi.class);
        return "成功（插入小组"+num+"条）";
    }

    @Override
    public String addCommonGroup(String poiPath, String groupPath) {
        List<Poi> poiList = ExcelUtils.readExcel(poiPath, Poi.class);
        List<Poi> poiList1 = ExcelUtils.readExcel(groupPath, Poi.class);
        if(poiList==null||poiList1==null){
            return "文件不存在";
        }
        List<UserAreaGroup> groupList=new ArrayList<>();
        int num=0;
        try {
            for (Poi poi : poiList) {
                if(poi.getUserId()==null||poi.getUserId()==-3/*||poi.getCategory()!=2*/){
                    continue;
                }
                for (Poi poi1 : poiList1) {
                    if(!poi.getAreaId().equals(poi1.getAreaId())){
                        continue;
                    }
                    UserAreaGroup userAreaGroup=new UserAreaGroup();
                    userAreaGroup.setUserId(Long.valueOf(poi.getUserId()));
                    userAreaGroup.setAreaId(Long.valueOf(poi1.getAreaId()));
                    userAreaGroup.setVillageId(Long.valueOf(poi1.getVillageId()));
                    userAreaGroup.setGroupId(Long.valueOf(poi1.getGroupId()));
                    userAreaGroup.setDelFlag(0);
                    groupList.add(userAreaGroup);
                    if(groupList.size()%1000==0){
                        num += userAreaGroupMapper.batchInsert(groupList);
                        groupList.clear();
                    }
                    break;
                }
            }
            if(groupList.size()>0){
                num += userAreaGroupMapper.batchInsert(groupList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "成功（插入小组："+num+"条）";
    }

    @Override
    public String addLibraryGroup(String poiPath, String groupPath) {
        List<Poi> poiList = ExcelUtils.readExcel(poiPath, Poi.class);
        List<Poi> poiList1 = ExcelUtils.readExcel(groupPath, Poi.class);
        if(poiList==null||poiList1==null){
            return "文件不存在";
        }
        List<UserAreaGroup> groupList=new ArrayList<>();
        int num=0;
        try {
            for (Poi poi : poiList) {
                if(poi.getUserId()==null||poi.getUserId()==-3){
                    continue;
                }
                for (Poi poi1 : poiList1) {
                    if(!poi.getVillageId().equals(poi1.getVillageId())){
                        continue;
                    }
                    UserAreaGroup userAreaGroup=new UserAreaGroup();
                    userAreaGroup.setUserId(Long.valueOf(poi.getUserId()));
                    userAreaGroup.setAreaId(Long.valueOf(poi.getAreaId()));
                    userAreaGroup.setVillageId(Long.valueOf(poi1.getVillageId()));
                    userAreaGroup.setGroupId(Long.valueOf(poi1.getGroupId()));
                    userAreaGroup.setDelFlag(0);
                    groupList.add(userAreaGroup);
                }
                if(groupList.size()>=1000){
                    num += userAreaGroupMapper.batchInsert(groupList);
                    groupList.clear();
                }
            }
            if(groupList.size()>0){
                num += userAreaGroupMapper.batchInsert(groupList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "成功（插入小组："+num+"条）";
    }
}
